package com.huang.juctool.CountDown;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;

public class SemaphoreDema {
    public static void main(String[] args) throws InterruptedException {
        Semaphore semaphore = new Semaphore(3); //相当于停车位 只有3个
        for (int i = 0; i < 10; i++) {
            new Thread(()->{
                try {
                    semaphore.acquire();//获得车位

                    TimeUnit.SECONDS.sleep(3);//停车  占位置

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName()+"获得了");
                semaphore.release();

            }).start();

        }




        }
    }

